package com.inwavetech.os;

import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import com.inwavetech.os.entity.Os;
import com.inwavetech.os.entity.User;
import com.inwavetech.os.util.DaoHelper;
import com.inwavetech.os.util.SessionManager;
import com.j256.ormlite.android.apptools.OrmLiteBaseActivity;
import com.j256.ormlite.dao.Dao;

import java.sql.SQLException;

/**
 * Created by jgskin on 2/18/14.
 */
public abstract class BaseOsCheckActivity extends OrmLiteBaseActivity<DaoHelper> {

    public static final String IE_OS_ID = "intent_extra_os_id";

    protected SessionManager sessionManager;
    protected User user;
    protected Os os;
    protected Dao osDao;

    protected boolean checkOsOwner(Os os) {
        User user = getUser();

        if (user != null) {
            if (os.isOwner(user)) {
                return true;
            } else {
                Log.i(getClass().getName(), "non authorized user attempt to finish os");
            }
        }

        return false;
    }

    protected void backToMain() {

    }

    protected User getUser() {
        if (!getSessionManager().checkLogin()) {
            finish();
            return null;
        }

        if (user == null) {
            try {
                user = sessionManager.getUser();
            } catch (SessionManager.SessionManagerException e) {
                logError(e);
            }
        }

        return user;
    }

    protected void logError(Exception e) {
        Log.e(getClass().getName(), String.format("%s - %s", e.getClass().getName(), e.getMessage()));
    }

    protected SessionManager getSessionManager() {
        if (sessionManager == null) {
            sessionManager = new SessionManager(this, getHelper());
        }

        return sessionManager;
    }

    protected Os getOs() {
        if (os == null) {
            // get the os id
            Intent intent = getIntent();
            int osId = intent.getIntExtra(IE_OS_ID, 0);

            try {
                os = (Os) getOsDao().queryForId(osId);
            } catch (SQLException e) {
                logError(e);

                databaseErrorMessage();
            }
        }

        return os;
    }

    protected void databaseErrorMessage() {
        // let the user know about the database error
        userMessage(getString(R.id.err_database));
    }

    protected Os getUserOs() {
        // query the database for the os
        Os os = getOs();

        if (os == null) {
            return null;
        }

        // get the user
        User user = getUser();

        if (user == null) {
            return null;
        }

        // check ownership
        if (!checkOsOwner(os)) {
            return null;
        }

        // all ok
        return os;
    }

    public Intent buildFinishIntent(int osId) {
        Intent intent = new Intent(this, FinishOsActivity.class);
        intent.putExtra(FinishOsActivity.IE_OS_ID, osId);

        return intent;
    }

    public void gotoSignatureActivity(int osId) {
        Intent intent = new Intent(this, SignatureActivity.class);
        intent.putExtra(SignatureActivity.IE_OS_ID, osId);

        startActivity(intent);
    }

    public Intent buildOsDetailIntent(int osId) {
        Intent intent = new Intent(this, OsDetailActivity.class);
        intent.putExtra(OsDetailActivity.IE_OS_ID, osId);

        return intent;
    }

    protected void userMessage(String message) {
        Toast.makeText(this, message, Toast.LENGTH_LONG).show();
    }

    protected boolean updateOs(Os os) {
        try {
            getOsDao().update(os);
        } catch (SQLException e) {
            logError(e);
            databaseErrorMessage();

            return false;
        }

        return true;
    }

    protected Dao getOsDao() {
        if (osDao == null) {
            osDao = getHelper().getDao(Os.class);
        }

        return osDao;
    }
}
